﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace YJKRevitDimensionText.Utility
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class Helper
	{
		public static List<List<string>> SpilitStr(string[] arrayStr, double lineWidth, double fontSize)
		{
			List<List<string>> list = new List<List<string>>();
			for (int i = 0; i < arrayStr.Length; i++)
			{
				List<string> list2 = new List<string>();
				string text = arrayStr[i];
				int num = (int)((double)Encoding.Default.GetBytes(text).Length * fontSize / 2.0 / lineWidth);
				if (num == 0)
				{
					num = 1;
				}
				int num2 = 0;
				if (num2 == 0)
				{
					num2 = text.Length / num;
				}
				int num3 = 0;
				for (int j = 0; j < num; j++)
				{
					try
					{
						if (num3 + num2 < text.Length)
						{
							string item = text.Substring(num3, num2);
							list2.Add(item);
						}
						else
						{
							string item2 = text.Substring(num3, text.Length);
							list2.Add(item2);
						}
					}
					catch (Exception)
					{
						break;
					}
					num3 += num2;
				}
				list.Add(list2);
			}
			return list;
		}

		public static string ReadTextMaxLenth(System.Windows.Forms.TextBox tb)
		{
			string result = null;
			int num = 0;
			string[] lines = tb.Lines;
			for (int i = 0; i < lines.Length; i++)
			{
				int num2 = Encoding.Default.GetBytes(lines[i]).Length;
				if (num2 > num)
				{
					num = num2;
					result = lines[i].ToString();
				}
			}
			return result;
		}

		public static XYZ Rotate(XYZ vect, XYZ axis, double angle)
		{
			double x = axis.X;
			double y = axis.Y;
			double z = axis.Z;
			double[,] array = new double[3, 3];
			array[0, 0] = x * x;
			array[0, 1] = x * y;
			array[0, 2] = x * z;
			array[1, 0] = y * x;
			array[1, 1] = y * y;
			array[1, 2] = y * z;
			array[2, 0] = z * x;
			array[2, 1] = z * y;
			array[2, 2] = z * z;
			double[,] array2 = array;
			double[,] array3 = new double[3, 3];
			array3[0, 1] = -z;
			array3[0, 2] = y;
			array3[1, 0] = z;
			array3[1, 2] = -x;
			array3[2, 0] = -y;
			array3[2, 1] = x;
			double[,] array4 = array3;
			double[,] array5 = new double[3, 3];
			array5[0, 0] = 1.0 - array2[0, 0];
			array5[0, 1] = -array2[0, 1];
			array5[0, 2] = -array2[0, 2];
			array5[1, 0] = -array2[1, 0];
			array5[1, 1] = 1.0 - array2[1, 1];
			array5[1, 2] = -array2[1, 2];
			array5[2, 0] = -array2[2, 0];
			array5[2, 1] = -array2[2, 1];
			array5[2, 2] = 1.0 - array2[2, 2];
			double[,] array6 = array5;
			double[,] array7 = new double[3, 3];
			array7[0, 0] = array2[0, 0] + Math.Cos(angle) * array6[0, 0] + Math.Sin(angle) * array4[0, 0];
			array7[0, 1] = array2[0, 1] + Math.Cos(angle) * array6[0, 1] + Math.Sin(angle) * array4[0, 1];
			array7[0, 2] = array2[0, 2] + Math.Cos(angle) * array6[0, 2] + Math.Sin(angle) * array4[0, 2];
			array7[1, 0] = array2[1, 0] + Math.Cos(angle) * array6[1, 0] + Math.Sin(angle) * array4[1, 0];
			array7[1, 1] = array2[1, 1] + Math.Cos(angle) * array6[1, 1] + Math.Sin(angle) * array4[1, 1];
			array7[1, 2] = array2[1, 2] + Math.Cos(angle) * array6[1, 2] + Math.Sin(angle) * array4[1, 2];
			array7[2, 0] = array2[2, 0] + Math.Cos(angle) * array6[2, 0] + Math.Sin(angle) * array4[2, 0];
			array7[2, 1] = array2[2, 1] + Math.Cos(angle) * array6[2, 1] + Math.Sin(angle) * array4[2, 1];
			array7[2, 2] = array2[2, 2] + Math.Cos(angle) * array6[2, 2] + Math.Sin(angle) * array4[2, 2];
			double[,] array8 = array7;
			return new XYZ(array8[0, 0] * vect.X + array8[0, 1] * vect.Y + array8[0, 2] * vect.Z, array8[1, 0] * vect.X + array8[1, 1] * vect.Y + array8[1, 2] * vect.Z, array8[2, 0] * vect.X + array8[2, 1] * vect.Y + array8[2, 2] * vect.Z);
		}

		public static void SetWitdth(TextNote textNote)
		{
			double num = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0;
			double num2 = textNote.TextNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			byte[] bytes = Encoding.Default.GetBytes(textNote.Text);
			double num3 = num * num2;
			double num4 = (double)bytes.Length * num3 / 1.7;
			textNote.Width = num4 + 1.5;
		}

		public static double FontSize(TextNoteType textNoteType)
		{
			double num = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * 100.0;
			double num2 = textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			return num * num2;
		}

		public static Element GetSelectType<T>(Autodesk.Revit.DB.Document doc, string selectTypeStr)
		{
			try
			{
				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(T)).ToElements())
				{
					if (element is T && element.Name.StartsWith(selectTypeStr))
					{
						return element;
					}
				}
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
			}
			return null;
		}

		public static void TxtSetNum(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == ' ')
			{
				e.KeyChar = '\0';
			}
			if (e.KeyChar == '-' && ((System.Windows.Forms.ComboBox)sender).Text.Length == 0)
			{
				return;
			}
			if (e.KeyChar > ' ')
			{
				try
				{
					double.Parse(((System.Windows.Forms.ComboBox)sender).Text + e.KeyChar.ToString());
				}
				catch
				{
					e.KeyChar = '\0';
				}
			}
		}

		public static void TxtSet(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == ' ')
			{
				e.KeyChar = '\0';
			}
			if (e.KeyChar == '-' && ((System.Windows.Forms.TextBox)sender).Text.Length == 0)
			{
				return;
			}
			if (e.KeyChar > ' ')
			{
				try
				{
					double.Parse(((System.Windows.Forms.TextBox)sender).Text + e.KeyChar.ToString());
				}
				catch
				{
					e.KeyChar = '\0';
				}
			}
		}

		public static void AddCombItems(List<string> levelList, System.Windows.Forms.ComboBox cb)
		{
			if (levelList != null)
			{
				foreach (string item in levelList)
				{
					cb.Items.Add(item);
				}
				if (cb.Items.Count > 0)
				{
					cb.SelectedIndex = 0;
				}
			}
		}

		public static List<string> GetConduitTypeStr(Autodesk.Revit.DB.Document doc, BuiltInCategory category)
		{
			List<string> result;
			try
			{
				List<string> list = new List<string>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(ConduitType));
				filteredElementCollector.OfCategory(category);
				foreach (Element element in filteredElementCollector.ToElements())
				{
					list.Add(element.Name);
				}
				result = list;
			}
			catch (Exception ex)
			{
				ex.Message.ToString();
				result = null;
			}
			return result;
		}

		public static bool ViewValidating(Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.View activeView, ref string message)
		{
			if (activeView.GetType() == typeof(ViewPlan))
			{
				ViewPlan viewPlan = activeView as ViewPlan;
				if (viewPlan.SketchPlane == null)
				{
					Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewPlan.Origin, viewPlan.RightDirection, viewPlan.UpDirection);
					Transaction transaction = new Transaction(doc);
					transaction.Start("new sketchPlane");
					SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(doc, plane);
					activeView.SketchPlane = sketchPlane;
					transaction.Commit();
				}
				return true;
			}
			if (activeView.GetType() == typeof(ViewSection))
			{
				ViewSection viewSection = activeView as ViewSection;
				if (viewSection.SketchPlane == null)
				{
					Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewSection.Origin, viewSection.RightDirection, viewSection.UpDirection);
					Transaction transaction2 = new Transaction(doc);
					transaction2.Start("new sketchPlane");
					SketchPlane sketchPlane2 = RevitVersionFuncs.NewSketchPlane(doc, plane2);
					activeView.SketchPlane = sketchPlane2;
					transaction2.Commit();
				}
				return true;
			}
			message = "当前视图无法运行此命令";
			return false;
		}

		public const string ENDALIGN = "末端对齐";

		public const string MIDDLEALIGN = "居中对齐";

		public const string STARTALIGN = "始端对齐";

		public const string INLINEEND = "在线端";

		public const string INLINEMIDDLEALIGN = "齐线中";

		public const string INLINESTART = "齐线端";
	}
}
